﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoItX3Lib;
using CommonFunction;
using System.IO;

namespace Pokerist_BuyAndSell
{
    public partial class PokerBuying : Form
    {
        public PokerBuying()
        {
            InitializeComponent();

            InitForm();
        }

        AutoItX3 autoit = new AutoItX3();
        bool readyAction = false;
        int gameStatus = 1;
        
        const int MIN_WIDTH = 718;
        const int MAX_WIDTH = 965;

        /*
         * WIN7 - 64 - 190 
         * SCROLL 200
         */
        const int PROFILE_POS_X = 558;
        const int PROFILE_POS_Y = 263;
        const int BUY_ICON_POS_X = 404;
        const int BUY_ICON_POS_Y = 444;
        const int TRANSPORT_POS_X = 83;
        const int TRANSPORT_POS_Y = 447;
        const int CAR_POS_X = 244;
        const int CAR_POS_Y = 343;
        const int OK_POS_X = 608;
        const int OK_POS_Y = 459;
        const int CONFIRM_POS_X = 489;
        const int CONFIRM_POS_Y = 669;
        const int RESET_WINDOW_POS_X = 12;
        const int RESET_WINDOW_POS_Y = 297;
        

        /*
         * WIN7 - 64 - KHAC 190
         * SCROLL 300
        const int PROFILE_POS_X = 572;
        const int PROFILE_POS_Y = 366;
        const int BUY_ICON_POS_X = 417;
        const int BUY_ICON_POS_Y = 552;
        const int TRANSPORT_POS_X = 97;
        const int TRANSPORT_POS_Y = 555;
        const int CAR_POS_X = 252;
        const int CAR_POS_Y = 446;
        const int OK_POS_X = 624;
        const int OK_POS_Y = 566;
        const int CONFIRM_POS_X = 501;
        const int CONFIRM_POS_Y = 771;
        const int RESET_WINDOW_POS_X = 26;
        const int RESET_WINDOW_POS_Y = 404;
        */

        /* server: 112.213.85.66
         * SCROLL 320
        const int PROFILE_POS_X = 571;
        const int PROFILE_POS_Y = 383;
        const int BUY_ICON_POS_X = 418;
        const int BUY_ICON_POS_Y = 569;
        const int TRANSPORT_POS_X = 101;
        const int TRANSPORT_POS_Y = 572;
        const int CAR_POS_X = 250;
        const int CAR_POS_Y = 459;
        const int OK_POS_X = 607;
        const int OK_POS_Y = 572;
        const int CONFIRM_POS_X = 509;
        const int CONFIRM_POS_Y = 791;
        const int RESET_WINDOW_POS_X = 27;
        const int RESET_WINDOW_POS_Y = 423;
    */
        private void PokerBuying_Load(object sender, EventArgs e)
        {
            
        }

        public void InitForm()
        {
            Pokerist_BS_Common.Data_FilePath = Path.Combine(Application.StartupPath, "BuySellData.xml");

            AccessData data = new AccessData();
            DataSet ds = new DataSet();
            var reader = new System.Xml.XmlNodeReader(data.GetData_Pokerist_BS());
            ds.ReadXml(reader);            
            grdAccounts.DataSource = ds.Tables[0].DefaultView;
            grdAccounts.Columns[0].Width = 130;
            grdAccounts.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            gameStatus = 1;
        }

        private void ReSizeForm(bool minimize)
        {
            if (minimize)
            {
                this.grpInfo.Visible = false;
                this.Width = MIN_WIDTH;
            }
            else
            {
                this.Width = MAX_WIDTH;
                this.grpInfo.Visible = true;
            }
        }

        private void PokerBuying_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
                ReSizeForm(true);
            else if (e.KeyCode == Keys.F3)
                ReSizeForm(false);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var selectedRow = grdAccounts.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault();
            if (selectedRow == null) return;

            gameStatus = 2;
            btnLogin.Enabled = false;
            btnSignOut.Enabled = true;

            this.Text = selectedRow.Cells["Email"].Value.ToString() + " - Press F2(minimize) and F3(maximize)";

            var formLogin = this.browserMain.Document.Forms[0];
            formLogin.GetElementsByTagName("input").GetElementsByName("PokeristSignInForm[login]")[0].InnerText = selectedRow.Cells["Email"].Value.ToString();
            formLogin.GetElementsByTagName("input").GetElementsByName("PokeristSignInForm[password]")[0].InnerText = selectedRow.Cells["Password"].Value.ToString();

            formLogin.InvokeMember("submit");
            grdAccounts.Enabled = false;
        }

        private void browserMain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
                ReSizeForm(true);
            else if (e.KeyCode == Keys.F3)
                ReSizeForm(false);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.browserMain.Document.Window.ScrollTo(this.browserMain.Left, this.browserMain.Top + Convert.ToInt16(textBox2.Text));
            
            readyAction = true;
            panelRole.Enabled = false;
            btnStart.Enabled = false;
            btnStop.Enabled = true;

            if (radBuy.Checked)
            {
                timerBuy.Enabled = true;
                timerBuy.Start();
            }
            else
            {
                timerSell.Enabled = true;
                timerSell.Start();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            panelRole.Enabled = true;
            btnStart.Enabled = true;
            btnStop.Enabled = false;

            if (radBuy.Checked)
            {
                timerBuy.Enabled = false;
                timerBuy.Stop();
            }
            else
            {
                timerSell.Enabled = false;
                timerSell.Stop();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.browserMain.Document.Window.ScrollTo(this.browserMain.Left, Convert.ToInt16(textBox2.Text));
        }

        private void DoBuy()
        {
            readyAction = false;

            int MARGIN = this.browserMain.Document.GetElementsByTagName("HTML")[0].ScrollTop;

            //click profile icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, PROFILE_POS_X, PROFILE_POS_Y - MARGIN);

            //click buy icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, BUY_ICON_POS_X, BUY_ICON_POS_Y - MARGIN);

            //click transport icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, TRANSPORT_POS_X, TRANSPORT_POS_Y - MARGIN);

            //click luxury car icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, CAR_POS_X, CAR_POS_Y - MARGIN);
            autoit.Sleep(100);

            //click ok icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, OK_POS_X, OK_POS_Y - MARGIN);

            //click ok confirm icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, CONFIRM_POS_X, CONFIRM_POS_Y - MARGIN);

            //click reset window
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, RESET_WINDOW_POS_X, RESET_WINDOW_POS_Y - MARGIN);
                        
            readyAction = true;
        }

        private void DoSell()
        {
            readyAction = false;

            //click profile icon
            //autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, 561, 60);

            //click property icon
            //autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, 336, 222);
            //autoit.Sleep(1000);

            //click transport icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, 336, 222);
            autoit.Sleep(300);

            //click luxury car icon
            autoit.ControlClick(this.Text, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "PRIMARY", 1, 491, 470);
            
            readyAction = true;
        }

        private void timerBuy_Tick(object sender, EventArgs e)
        {
            if (!readyAction) return;
            DoBuy();
        }

        private void timerSell_Tick(object sender, EventArgs e)
        {
            if (!readyAction) return;
            DoSell();
        }

        private void browserMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (gameStatus == 2)
            {
                this.browserMain.Navigate("http://pokerist.com/en/play-now/");
                gameStatus = 3;
            }
            else if (gameStatus == 4)
            {
                this.browserMain.Navigate("http://pokerist.com/en/sign-in/");
                gameStatus = 1;
            }
        }

        private void btnSignOut_Click(object sender, EventArgs e)
        {
            var confirm = MessageBox.Show("Do you want to sign out the poker?", "Poker pro", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (confirm == System.Windows.Forms.DialogResult.Yes)
            {
                this.browserMain.Navigate(new Uri("http://pokerist.com/en/sign-out/"));
                btnLogin.Enabled = true;
                btnSignOut.Enabled = false;
                grdAccounts.Enabled = true;
                gameStatus = 4;
            }
        }
    }
}
